#!/usr/bin/env bash
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# Function :CentOS7.X  批量（drds服务器组内） 执行 命令
# Platform :RedHatEL7.x Based Platform
# Version  :1.01
# Date     :2021-08-04
# Author   :mugoLH
# Contact  :houxiaoshuai@baidu.com & hxsaj@126.com
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
#  变量列表 List of common variables
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
#  注意 egrep "mp|node"  /etc/hosts 是在/etc/hosts中遍历查询获得服务器列表，所以服务器名字修改，需要修改egrep "mp|node" 中的"mp|node"

#  主机名列表
HostList=$(egrep "drds|node" /etc/hosts |grep -v "^#" |awk '{print$2}')

#  函数列表 Function list
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
info_p(){    printf "\e[1;35;40m%-8s %-20s %-24s %-2s %-2s\n\e[0m" "$(date '+%H:%M:%S')" "${1}" "${2}" "${3}" "${4}" >&1 ;}

# 如果参数为空，则退出
if [ $# -eq 0 ];then
	exit
else
    #  循环远程执行命令
    for i in ${HostList[@]};do
    	HostIp=$(grep ${i} /etc/hosts | awk '{print$1}')
    	info_p "节点:${i}" "IP:${HostIp}" "# $@ "
    	ssh ${i} "$@"
    	echo ""
    done
fi
